from rest_framework.views import exception_handler as drf_exception_handler
from rest_framework.views import Response
from rest_framework import status
from utils.logging import get_logger


logger=get_logger('django')
# import logging
# logging.getLogger('django')
def exception_handler(exc, context):
    response = drf_exception_handler(exc, context)
    if response is None: # 有异常
        # 记录服务器异常
        logger.critical('%s' % exc)
        response = Response({'detail': '服务器异常，请重试...'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    return response




# 自定义异常类
from rest_framework.exceptions import  APIException

class MyException(APIException):
    status_code = 405
    default_detail = '校验出错了'
    default_code = '错误'

    def __init__(self, detail=None, code=None):
        super().__init__(detail=None, code=None)